/**
 * 會員參數獲取類以及設置類
 * 
 */
function userVars() {
	
}

/**
 * 設置用戶的擴展屬性
 * @param	r_user
 * @param	r_var  String 变量名
 * @param	r_type String  返回的数据类型 int | str | num | bool  
 */
userVars.prototype.getVar = function(r_user, r_var, r_type) {	
	if (String(r_user) == "undefined" ) {
		__debug.trace("##err: userVars::getVar(),  [r_user] is " + r_user );
		return;
	}
	var _vars = r_user.getVariable(r_var);			
	if (_vars != null) {
		var _value = _vars.value;
		switch(r_type) {
			case "int":		
				_value = parseInt(_value);
				break;
			case "str":
				_value = String(_value);
				break;
			case "num":
				_value = parseFloat(_value);
				break;
			case "bool":
				_value = Boolean(_value);
				break;				
		}		
		return _value;
	}else {
		return null;
	}	
}

/**
 * 設置用戶的擴展屬性
 * @param	r_user
 * @param	r_params
 */
userVars.prototype.setVar = function(r_user, r_params) {
	_server.setUserVariables(r_user, r_params, false);	
}

/**
 * 获取玩家状态
 * @return String   play | sit | spectator
 */
userVars.prototype.getState = function(r_user) {
	var _state = this.getVar(r_user, "state", "int");
	switch(_state) {
		case 3:
			return "play";
			break;
		case 2:
			return "sit";
			break;
		case 1:
			return "spectator";
			break;
	}
	return "no"; 	
}

/**
 * 设置玩家状态
 * @param r_user User 玩家对象
 * @param r_state String 	play | sit | spectator
 */
userVars.prototype.setState = function(r_user, r_state) {	
	switch(r_state) {
		case "play":
			_val = 3;
			break;
		case "sit":			
			_val = 2;
			break;
		case "spectator":
			_val = 1;
			break;			
		default :
			_val = 0;
	}
	this.setVar(r_user, { state: _val } );	
}


/**
 * 設置用戶的擴展屬性
 * @param	r_user
 * @param	r_params
 */
function setUserVars(r_user, r_params) {	
	_server.setUserVariables(r_user, r_params, false);	
}

/**
 * 獲取單個用戶的遊戲數據
 * @param	r_user
 * @return [{},.....]
 */
userVars.prototype.getUserVars = function(r_user) {	
	var _res = [];		
	_res.push(this.getVar(r_user, "userId", "int"));
	_res.push(this.getVar(r_user, "name", "str"));
	_res.push(this.getVar(r_user, "face", "str"));
	_res.push(this.getVar(r_user, "gold", "int"));
	_res.push(this.getVar(r_user, "level", "int"));
	_res.push(this.getVar(r_user, "label", "int"));
	if ( !r_user.isSpectator()) {
		_res.push(parseInt(r_user.getPlayerIndex()));
		_res.push(this.getVar(r_user, "chip", "int"));		
	}else {
		_res.push(0);
		_res.push(0);
	}
	return _res;
}

/**
 * 扣除玩家筹码
 * 
 */
userVars.prototype.subtractChip = function(r_user, r_chip) {
	var _chip = this.getVar(r_user, "chip", "int"); 
	_chip -=  r_chip;
	if (_chip < 0) {
		_chip = 0
	}	
	this.setVar(r_user, { chip : _chip } );	
	return _chip;
}

/**
 * 增加玩家筹码
 * 
 */
userVars.prototype.addChip = function(r_user, r_chip) {
	var _chip = this.getVar(r_user, "chip", "int"); 
	_chip += r_chip;
	this.setVar(r_user, { chip : _chip } );
}




/** ============================================**/





/**
 * 闲置用户
 * @param	r_user	待闲置用户
 */
function layUser(r_user) {
	if (r_user.getVariable("state") != undefined) {
		var _state = parseInt(r_user.getVariable("state").value);
		if (_state == 1) {			
			userOffGame(r_user);			
		}	
	}	
}

/**
 * 用戶離開遊戲
 * @param	r_user
 */
function userOffGame(r_user) {
	var _room = _zone.getRoom(r_user.getRoom());
	if ( _room && !r_user.isSpectator()) {
		if (_room.getVariable("state") == 2) {
			var _params = { };
			_params._cmd = 'userOff';//遊戲中有人掉線或登出
			clearSitIndex(_room, r_user.getPlayerIndex());
			__userEvent.setUserState(_room, r_user, 'spectator', false);			
		}
	}
}

/**
 * 用戶筹码动态变化
 * @param	r_user	用戶
 * @param	r_bet	下注數
 * @param	r_type	操作类型
 */
function userBet(r_user,r_bet,r_type) {
	var _bank = r_user.getVariable("bank");
	r_type = (r_type == null) ? 0 : r_type;
	if (_bank != undefined) {		
		_bank = parseInt(_bank.value);
		if (r_type == 1) {
			_bank = _bank + r_bet;			
		}else if (r_type == 0 && _bank >= r_bet) {
			_bank = _bank - r_bet;
		}
		var _obj = { bank:_bank };
		setUserVars(r_user, _obj);
		return true;
	}
	return false;	
}
